package com.xgimi.defaultapp;

import com.xgimi.defaultapp.view.MainUpView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver.OnGlobalFocusChangeListener;
import android.view.Window;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

	private static final String TAG = "DefaultApp";

	Context mContext;
	LinearLayout main_lay;
	MainUpView mMainUpView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		mContext = getApplicationContext();
		initAllViews();
	}

	@SuppressLint("NewApi")
	private void initAllViews() {
		// 获取壁纸管理器  
        WallpaperManager wallpaperManager = WallpaperManager  
                .getInstance(mContext);  
        // 获取当前壁纸  
        Drawable wallpaperDrawable = wallpaperManager.getDrawable(); 
		mMainUpView = (MainUpView) findViewById(R.id.mainUpView); 
		mMainUpView.setUpRectResource(R.drawable.test_rectangle);
		mMainUpView.setShadowResource(R.drawable.item_shadow);
		main_lay = (LinearLayout) findViewById(R.id.main_lay);
		main_lay.setBackground(wallpaperDrawable);
		main_lay.getViewTreeObserver().addOnGlobalFocusChangeListener(new OnGlobalFocusChangeListener() {
			@Override
			public void onGlobalFocusChanged(View oldFocus, View newFocus) {
				mMainUpView.setFocusView(newFocus, oldFocus, 1.2f);
			}
		});
		int count = main_lay.getChildCount();
		for (int i = 0; i < count; i++) {
			main_lay.getChildAt(i).setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					int id = v.getId();
					Intent intent = new Intent(mContext, DefaultActivity.class);
					switch (id) {
					case R.id.input_lay: // 输入法
						startActivity(intent);
						break;
					case R.id.video_lay: // 视频
						intent.putExtra("intent", initVideoApp());
						startActivity(intent);
						break;
					case R.id.music_lay: // 音乐.
						intent.putExtra("intent", initMusicApp());
						startActivity(intent);
						break;
					case R.id.web_lay: // 浏览器
						intent.putExtra("intent", initWebApp());
						startActivity(intent);
						break;
					case R.id.doc_lay: // 文档
						intent.putExtra("intent", initDocApp());
						startActivity(intent);
						break;
					case R.id.image_lay: // 图片
						intent.putExtra("intent", initImageApp());
						startActivity(intent);
						break;
					case R.id.launcher_lay: // 桌面
						intent.putExtra("intent", initLauncherApp());
						startActivity(intent);
						break;
					default:
						break;
					}
				}
			});
		}
	}

	private Intent initVideoApp() {
		Intent mainIntent = new Intent(Intent.ACTION_VIEW);
		Uri uri = Uri.parse("file:///");
		mainIntent.setDataAndType(uri, "video/*");
		return mainIntent;
	}

	private Intent initMusicApp() {
		Intent mainIntent = new Intent(Intent.ACTION_VIEW);
		Uri uri = Uri.parse("file:///");
		mainIntent.setDataAndType(uri, "audio/*");
		return mainIntent;
	}

	private Intent initWebApp() {
		Intent mainIntent = new Intent(Intent.ACTION_VIEW);
		mainIntent.setData(Uri.parse("http://"));
		return mainIntent;
	}

	private Intent initDocApp() {
		Intent mainIntent = new Intent(Intent.ACTION_VIEW);
		mainIntent.setType("text/plain");
		return mainIntent;
	}

	private Intent initImageApp() {
		Intent mainIntent = new Intent(Intent.ACTION_VIEW);
		mainIntent.setType("image/*");
		return mainIntent;
	}

	private Intent initLauncherApp() {
		Intent mainIntent = new Intent(Intent.ACTION_MAIN);
		mainIntent.addCategory(Intent.CATEGORY_HOME);
		mainIntent.addCategory(Intent.CATEGORY_DEFAULT);
		return mainIntent;
	}

}
